def prime(n):
    if n <= 1:
        return False
    if n <= 3:
        return True
    if n % 2 == 0 or n % 3 == 0:
        return False
    i = 5
    while i * i <= n:
        if n % i == 0 or n % (i + 2) == 0:
            return False
        i += 6
    return True

def most_prime_search(num):
    num = int(num)
    if not (1000 <= num <= 9999):
        return 0

    num_str = str(num)
    numbers = sorted(num_str, reverse=True)
    most_prime = 0

    for i in range(4):
        for j in range(i + 1, 4):
            current_num = int(numbers[i] + numbers[j])
            if prime(current_num):
                most_prime = max(most_prime, current_num)

    if most_prime == 0:
        current_num = int("".join(numbers))
        if prime(current_num):
            most_prime = current_num

    return most_prime

number_input = input()
result = most_prime_search(number_input)
print(result)
